/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package jfinance;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author efim
 */
public class AccountsController implements Controller{
    
    private AccountsModel model;
    
    private List<View> views;
    
    public AccountsController(AccountsModel model){
        this.model = model;
        views = new LinkedList<>();
        model.addPropertyChangeListener(this);
        for(Account a:model.getAccounts()){
            a.addPropertyChangeListener(this);
        }
    }
    
    public AccountsController(AccountsModel model,View view){
        this.model = model;
        views = new LinkedList<>();
        views.add(view);
        model.addPropertyChangeListener(this);
        for(Account a:model.getAccounts()){
            a.addPropertyChangeListener(this);
        }
    }
    
    public void addAccount(Account account){
        model.addAccount(account);
    }
    public void addAccount(String name) 
    {
        model.addAccount(name);
    }
    
    public void setAccountById(int id, Account account) 
    {
        model.setAccountById(id, account);
    }
    
    public void deleteAccountById(int id) 
    {
        model.deleteAccountById(id);
    }
    
    public void deleteAccount(Account account) 
    {
        model.deleteAccount(account);       
    }
    
    public LinkedList<Account> getAccounts(){
        return model.getAccounts();
    }
    
    public Account getAccountById(int id){
        return model.getAccountById(id);
    }
    
    public Transaction getTransactionById(int id,Account account){
        return account.getTransactionById(id);
    }
    
    public void addTransaction(Transaction transaction,Account account) 
    {
        account.addTransaction(transaction);
    }
    
    public void editTransactionById(int id,Transaction transaction,Account account) 
    {
        account.setTransactionById(id, transaction);
    }
    
    public void deleteTransaction(int id,Account account) 
    {
        account.deleteTransaction(id);
    }
    
    public void deleteTransaction(Transaction transaction,Account account) 
    {
        account.deleteTransaction(transaction);
    }
    
    
    public LinkedList<Transaction> getAllTransactions(){
        LinkedList<Transaction> results = new LinkedList<>();
        LinkedList<Account> list = model.getAccounts();
        for(Account g :list){
            results.addAll(g.getTransactions());
        }
        return results;
    }

    @Override
    public void propertyChange(PropertyChangeEvent pce) {
        for(View v:views){
            v.modelPropertyChange(pce);
        }
    }
    @Override
    public void addView(View view){
        if(views.contains(view))return;
        views.add(view);
    }

    @Override
    public void removeView(View view) {
        if(!views.contains(view))return;
        views.remove(view);
    }

       
    
}
